<template>
    <div class="layout-content">
        <div class="layout-content-main">
            <div class="content-div">
                <Row style="margin-bottom:5px;">
                    <Col span="21" class="table-form-input-select-col">
                    <span class="alltitle">帐号管理</span>
                    </Col>
                    <Col span="3" class="table-form-input-select-col" order="4">
                    <Button type="primary" icon="ios-book" size="large" @click="addData">新增主管帐号</Button>
                    </Col>
                  <!--   <Col span="2" class="table-form-input-select-col" order="4">
                  <Button type="error" icon="ios-trash" size="large" @click="deleteall">删除</Button>
                  </Col> -->
                </Row>
            </div>
            <!--    <hr class="hr1" /> -->
            <div class="content-div">
                <Table border :columns="columns2" :data="data" ref="table" @on-expand="expand" @on-selection-change="selectChange"></Table>
            </div>
        </div>
        <div>
            <Modal v-model="showDataModalFlag" title="新增操作人" @on-ok="add_operator('formValidate')" :mask-closable="false">
                <Form :label-width="100" ref="formValidate" :rules="ruleValidate" :model="formValidate">
                    <FormItem label="帐号" prop="coname">
                        <Input placeholder="输入帐号" class="input-select-class" v-model="formValidate.coname"></Input>
                    </FormItem>
                    <FormItem label="密码" prop="password">
                        <Input placeholder="输入密码" class="input-select-class" v-model="formValidate.password"></Input>
                    </FormItem>
                </Form>
            </Modal>
            <Modal v-model="showDataModalFlag1" title="新增客户帐号" @on-ok="add_customer('formValidate')" :mask-closable="false">
                <Form :label-width="100" ref="formValidate" :rules="ruleValidate" :model="formValidate">
                    <FormItem label="帐号" prop="coname">
                        <Input placeholder="输入帐号" class="input-select-class" v-model="formValidate.coname"></Input>
                    </FormItem>
                    <FormItem label="密码" prop="password">
                        <Input placeholder="输入密码" class="input-select-class" v-model="formValidate.password"></Input>
                    </FormItem>
                </Form>
            </Modal>
        </div>
        <Modal v-model="addDataModalFlag" title="新增主管帐号" @on-ok="handleSubmit('formValidate')" :mask-closable="false">
            <Form :label-width="100" ref="formValidate" :rules="ruleValidate" :model="formValidate">
                <FormItem label="帐号" prop="coname">
                    <Input placeholder="输入帐号" class="input-select-class" v-model="formValidate.coname"></Input>
                </FormItem>
                <FormItem label="密码" prop="password">
                    <Input placeholder="输入密码" class="input-select-class" v-model="formValidate.password"></Input>
                </FormItem>
            </Form>
        </Modal>
    </div>
</template>
<script>
import expandRow from '../components/expand.vue';
export default {
    components: {
        expandRow
    },
    data() {
        return {
            formValidate: {
                coname: '',
                password: '',

            },
            ruleValidate: {
                coname: [{
                    required: true,
                    message: '帐号名不能为空',
                    trigger: 'blur'
                }],
                password: [{
                    required: true,
                    message: '密码不能为空',
                    trigger: 'blur'
                }],


            },
            showDataModalObject: {},
            showDataModalObject1: {},
            addDataModalObject: {},
            showDataModalFlag: false,
            showDataModalFlag1: false,
            addDataModalFlag: false,

            columns2: [{
                    title: '序号',
                    key: 'id',
                    width: 80,
                    type: "selection"
                }, {
                    type: 'expand',
                    width: 50,
                    render: (h, params) => {
                        return h(expandRow, {
                            props: {
                                row: params.row
                            }
                        })
                    }
                }, {
                    title: '主管帐号',
                    key: 'supervisor_name',
                    width: 100
                }, {
                    title: '主管密码',
                    key: 'supervisor_password',
                }, {
                    title: '操作',
                    key: 'action',
                    width: 211,
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'info',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.showData(params.index)
                                    }
                                }
                            }, '新增操作人'),
                            h('Button', {
                                props: {
                                    type: 'info',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.showData1(params.index)
                                    }
                                }
                            }, '新增客户帐号'),
                        ])
                    }
                }

            ],
            data: [],
            pageData: {
                current: 1,
                total: 0,
                pageSize: 20
            }
        }
    },
    created() {

        var self = this
        $.ajax({
            url: "https://evp.51job.com/51job/api/51miniadmin/back.php?action=userlist",
            type: "POST",
            dataType: "json",
            success: function(data) {

                console.log(data);

                if (data.data != null) {

                    self.data = data.data
                    self.pageData.total = parseInt(data.length)
                    self.pageData.pageSize = 20
                    self.current = 1

                } else {

                     self.$Message.error('没有用户');

                }

            }
        });
    },
    methods: {
        changepage(currentPage) {
            if (isNaN(currentPage)) {
                currentPage = 1
            }
            var self = this
                /*self.$api.get('/api/v1/table_data', null, function (result) {
                  self.data = result.resultData.dataList
                  self.pageData = result.resultData.pageData
                })*/
            var searchdata = {};


            console.log(searchdata)

            $.ajax({
                url: "https://evp.51job.com/51job/api/51miniadmin/back.php?action=userlist",
                type: "POST",
                dataType: "json",
                success: function(data) {

                    console.log(data);

                    if (data.data != null) {

                        self.data = data.data
                        self.pageData.total = parseInt(data.datatotal)
                        self.pageData.pageSize = 20
                        self.current = currentPage

                    } else {

                        alert("没有用户")

                    }

                }
            });
        },
        showData(index) {
            var self = this
            self.showDataModalFlag = true
            self.showDataModalObject = self.data[index]
            console.log(self.showDataModalObject.supervisor_id)
        },
        showData1(index) {
            var self = this
            self.showDataModalFlag1 = true
            self.showDataModalObject1 = self.data[index]
            console.log(self.showDataModalObject1.supervisor_id)
        },
        addData(index) {
            var self = this
            self.addDataModalFlag = true
            self.addDataModalObject = self.data[index]
        },
        changeDate(dateA) {

            var dateee = new Date(dateA).toJSON()

            var date = new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')

            return date
        },
        handleSubmit(name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    var self = this
                    //console.log(self.changeDate(self.formValidate.sdate))

                    $.ajax({
                        url: "https://evp.51job.com/51job/api/51miniadmin/back.php?action=usercreate",
                        data: {
                            "username": self.formValidate.coname,
                            "password": self.formValidate.password,
                            "authorize": 1,
                        },
                        type: "POST",
                        dataType: "json",
                        success: function(data) {

                            console.log(data);
                            self.$router.push({
                                path: '/帐号管理'
                            })
                            self.$Message.success('添加成功!')
                            self.changepage(1);

                        }
                    });


                } else {
                    this.$Message.error('表单验证失败!')
                }
            })

        },
        add_operator(name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    var self = this
                    console.log
                    //console.log(self.changeDate(self.formValidate.sdate))

                    $.ajax({
                        url: "https://evp.51job.com/51job/api/51miniadmin/back.php?action=usercreate",
                        data: {
                            "username": self.formValidate.coname,
                            "password": self.formValidate.password,
                            "authorize": 2,
                            "under_by":self.showDataModalObject.supervisor_id
                        },
                        type: "POST",
                        dataType: "json",
                        success: function(data) {

                            console.log(data);
                            self.$router.push({
                                path: '/帐号管理'
                            })
                            self.$Message.success('添加成功!')
                            self.changepage(1);

                        }
                    });


                } else {
                    this.$Message.error('表单验证失败!')
                }
            })

        },
        add_customer(name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    var self = this
                    console.log
                    //console.log(self.changeDate(self.formValidate.sdate))

                    $.ajax({
                        url: "https://evp.51job.com/51job/api/51miniadmin/back.php?action=usercreate",
                        data: {
                            "username": self.formValidate.coname,
                            "password": self.formValidate.password,
                            "authorize": 3,
                            "under_by":self.showDataModalObject1.supervisor_id
                        },
                        type: "POST",
                        dataType: "json",
                        success: function(data) {

                            console.log(data);
                            self.$router.push({
                                path: '/帐号管理'
                            })
                            self.$Message.success('添加成功!')
                            self.changepage(1);

                        }
                    });


                } else {
                    this.$Message.error('表单验证失败!')
                }
            })

        },





        removeData(index) {
            var self = this
            self.$api.post('/api/v1/edit', self.data[index], function(result) {
                self.data.splice(index, 1)
                self.$Message.info(result.message)
            })
        },
        exportData(type) {

            this.$refs.table.exportCsv({
                filename: 'The original data'
            });

        },
        selectChange(selection, index) {
            this.multipleSelection = selection;

        },
        expand(selection, index) {

            //console.log(selection.coname,index)

        },
        deleteall() {

            //console.log(this.multipleSelection);
            if (this.multipleSelection == null) {
                this.$Message.error('请至少勾选一项');
            } else {

                this.$Modal.confirm({
                    title: '确认删除？',
                    content: '',
                    onOk: () => {
                        var arr = this.multipleSelection;
                        console.log(arr)
                        var self = this;
                        self.$api.post('/api/v1/edit', self.data, function(result) {

                            let add = self.data.filter(item => !arr.some(ele => ele.id === item.id));
                            self.data = add;

                            var jobarr = [];
                            for (var i = 0; i < arr.length; i++) {

                                jobarr.push(arr[i].id);
                            }
                            console.log(jobarr)
                            $.ajax({
                                url: "http://evp.51job.com/51job/api/51jobtrain/back_api.php?action=delad",
                                data: {
                                    "id[]": jobarr
                                },
                                type: "POST",
                                dataType: "json",
                                success: function(data) {

                                    console.log(data);
                                    self.changepage(1);

                                }
                            });

                        })
                    },
                    onCancel: () => {
                        this.$Message.info('取消删除');
                    }
                });




            }
        },
    }
}
</script>
